<template>
  <!--登录表单容器-->
  <div class="login-container">
    <!--登录表单卡片-->
    <el-card class="login-card">
      <!--卡片标题-->
      <template #header>
        <div class="login-header">
          <span>音乐管理系统登录</span>
        </div>
      </template>

      <!--登陆表单-->
      <el-form
        :model="loginForm"
        :rules="loginRules"
        ref="loginFormRef"
        class="login-form"
        @submit.prevent="handleLogin"
      >
        <!--账号输入框-->
        <el-form-item prop="username">
          <el-input
            placeholder="请输入管理员账号"
            v-model="loginForm.username"
            clearable
          >
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <!--密码输入框-->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入管理员密码"
            show-password
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <!--登录按钮-->
        <el-form-item>
          <el-button
            type="primary"
            native-type="submit"
            :loading="loading"
            class="login-button"
            block
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import api from '@/services/api'

export default {
  name: 'Login',
  components: {
    User,
    Lock
  },
  setup() {
    const router = useRouter()

    const loginFormRef = ref(null)

    // 表单数据
    const loginForm = reactive({
      username: '',
      password: ''
    })

    // 登录表单的验证规则
    const loginRules = {
      username: [
        { required: true, message: '请输入管理员账号', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入管理员密码', trigger: 'blur' },
        { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
      ]
    }

    // 加载状态
    const loading = ref(false)

    // 登录处理函数
    const handleLogin = async () => {
      if (!loginFormRef.value) return

      try {
        const valid = await loginFormRef.value.validate()
        if ( valid){
          loading.value = true

          const response = await api.admin.login({
            name: loginForm.username,
            password: loginForm.password
          })

          if (response.code === '1'){
            ElMessage.success('登录成功')
            localStorage.setItem('adminToken', response.data.token)
            localStorage.setItem('adminName', response.data.name)
            router.push('/dashboard')
          }else {
            ElMessage.error('登录失败')
          }
        }
      } catch ( error){
        console.error('登录错误',error)
        if (error.response){
          ElMessage.error(`登陆失败：' ${error.response.data.msg || '服务器错误'}`)
        } else {
          ElMessage.error('网络连接错误，请检查网络')
        }
      }finally {
        loading.value = false
      }
    }

    return {
      loginFormRef,
      loginForm,
      loginRules,
      loading,
      handleLogin
    }
  }
}
</script>

<style scoped>
/* 登录页面容器样式 */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: lightskyblue;
}

/* 登录卡片样式 */
.login-card {
  width: 400px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

/* 登录卡片头部样式 */
.login-header {
  text-align: center;
  font-size: 20px;
  font-weight: bold;
  color: #333;
}

/* 登录表单样式 */
.login-form {
  margin-top: 20px;
}

/* 登录按钮样式 */
.login-button {
  margin-top: 20px;
  width: 100%;
}
</style>
